package cn.wolfcode.controller;

import cn.wolfcode.domain.Emp;
import cn.wolfcode.domain.Result;
import cn.wolfcode.service.EmpService;
import cn.wolfcode.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {
    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        Emp emp1 = empService.login(emp);
        if (emp1 != null){
            Map<String, Object> map = new HashMap<>();
            map.put("username",emp1.getUsername());
            map.put("id",emp1.getId());
            return Result.success(JwtUtils.generateJwt(map));
        }else {
            return Result.error("用户名或密码错误");
        }
    }
}
